//
//  topViewController.m
//  timemovie
//
//  Created by Yiqiao on 15/7/17.
//  Copyright (c) 2015年 Yiqiao. All rights reserved.
//

#import "topViewController.h"
#import "CollectionViewCell.h"
#import "collectModel.h"
#import "MObj.h"
#define w [UIScreen mainScreen].bounds.size.width
#define h [UIScreen mainScreen].bounds.size.height
#define navH 64;
#define barH 49;
@interface topViewController ()

@end

@implementation topViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self =[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"Top250";
    }
    return self;
}

- (void)_initData{
    movieInfoArray = [[NSMutableArray alloc]init];
}

- (void)_loadData{
    
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"top250.json" ofType:nil];
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    
    NSDictionary *dictionary =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    
    NSArray *subjects = [dictionary objectForKey:@"subjects"];
//    
//    for (NSDictionary *dic in subjects) {
//        collectModel *collection = [[collectModel alloc]init];
//        [collection setValuesForKeysWithDictionary:dic];
//        [movieInfoArray addObject:collection];
//    }
//    
    
    for (NSDictionary *dictionary in subjects) {
        
      //  NSDictionary *subject = [dictionary objectForKey:@"subjects"];
        MObj *model = [[MObj alloc]init];
        model.title = [dictionary objectForKey:@"title"];
        model.year = [dictionary objectForKey:@"year"];
        model.images = [dictionary objectForKey:@"images"];
        NSNumber *number = [[dictionary objectForKey:@"rating"]objectForKey:@"average"];
        model.average = [number floatValue];
        [movieInfoArray addObject:model];
        NSLog(@"%@",model.images);
        
    }
}
- (void)_createCollectionView{
    
    UICollectionViewFlowLayout *cvfLayout = [[UICollectionViewFlowLayout alloc]init];
    cvfLayout.minimumLineSpacing = 20;
    cvfLayout.minimumInteritemSpacing = 5;
    cvfLayout.itemSize = CGSizeMake((w-40)/3, (w-40)*1.5/3);

    _collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:cvfLayout];
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    UINib *nib = [UINib nibWithNibName:@"CollectionViewCell" bundle:[NSBundle mainBundle]];
    [_collectionView registerNib:nib forCellWithReuseIdentifier:@"cell"];
    
    [self.view addSubview:_collectionView];

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return movieInfoArray.count;
}

- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    MObj *model =[[MObj alloc]init];
    model = movieInfoArray[indexPath.row];
    cell.model = model;
    
    return cell;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self _createCollectionView];
    [self _initData];
    
    [self _loadData];
    
  //  self.view.backgroundColor = [UIColor greenColor];
 //   self.title = @"Top";
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
